var tableIns;
var tableIns1;

layui.extend().use(['jquery','layer','form','table','util','laydate'],function () {
    var $ = layui.jquery,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        util = layui.util,
        laydate = layui.laydate;

    tableIns = table.render({
        elem: '#userTable',
        url: '/warehouse/selbaoquedan',
        title: '报缺药品表',
        toolbar: '#userToolBar',
        height:'full-100',
        cellMinWidth: 150,
        page: 1,
        limits: [10,20,30],
        limit: 10,
        cols: [[
            // {field: 'baoqueid',title: 'ID', width: '20%',align:'center'},
            {field: 'baoqueName',title: '报缺药品', width: '40%',align:'center'},
            {field: 'baoqueNum',title: '需求', width: '40%',align:'center'},
            {fixld: 'right', toolbar:'#userBar',title: '操作', width: '20%',align:'center'}
        ]]
    });

    tableIns = table.render({
        elem: '#userTable1',
        url: '/warehouse/selectdrugstore',
        title: '库房药品表',
        toolbar: '#userToolBar1',
        height:'full-100',
        cellMinWidth: 150,
        page: 1,
        limits: [10,20,30],
        limit: 10,
        cols: [[
            {field: 'warehouseName',title: '库房', width: '18%',align:'center'},
            {field: 'drugstoreName',title: '仓库名称', width: '20%',align:'center'},
            {field: 'supplierName',title: '供货商', width: '21%',align:'center'},
            {field: 'areaName',title: '产地', width: '23%',align:'center'},
            {field: 'drugstorenum',title: '仓库数量', width: '16%',align:'center'},
            {fixld: 'right',title: '操作', toolbar: "#userBar1", width: '100',align:'center'}
        ]]
    });

    $("#doSearch").click(function (){
        var param = $("#searchFrm").serialize();
        tableIns.reload({
            url:"/warehouse/selectdrugstore?"+param
        });
    });

    // 出库
    table.on('tool(userTable1)',function (obj){
       var data = obj.data;
       var layEvent = obj.event;
       if(layEvent=='add'){
           var produceDate = data.produceDate;
           var validDate = data.validDate;
           var date = new Date();
           var durnum = data.drugstorenum;

           layer.confirm('确定出库【'+data.drugstoreName+'】药品？',function (index){
                layer.prompt({
                    title:'请输入数量',
                    formType: 0,
                    value: '',
                    maxlength: 140
                },function (value,index,elem){
                    if(value > durnum){
                        layer.msg("数量不足");
                    }else{
                        var jbr = $("#jingbanren").val();
                        $.post('/warehouse/addpharmacy',{
                                rugstoreId: data.rugstoreId,
                                warehouseId: data.warehouseId,
                                warehouse: data.warehouseId,
                                pharmacyName: data.drugstoreName,
                                unit: data.unit,
                                sellingPrice: data.sellingPrice,
                                area: data.area,
                                produceDate: produceDate,
                                validDate: validDate,
                                skullbatch: data.batch,
                                type: data.type,
                                jiluname: data.drugstoreName,
                                jilutype: "正常出库",
                                jilupihao: data.batch,
                                jilunumber: value,
                                drugstorenum: value
                            },
                            function (res){
                                if (res == 1) {
                                    layer.msg("出库成功");
                                    tableIns.reload();

                                    layer.close(index);

                                } else {
                                    layer.msg("出库失败");
                                    layer.close(index);
                                    tableIns.reload();

                                }
                                setTimeout(function () {

                                    location.reload();
                                },1000)
                        });
                    }
                });
           });
       }
    });

    // 补给
    table.on('tool(userTable)',function (obj){
        var data = obj.data;
        var layEvent = obj.event;
        if(layEvent=='add'){
            layer.confirm('是否补给【'+data.baoqueName+'】药品？',function (index){
                $("#bqnum").val(data.baoqueNum);
                openUpdate(data);
                layer.closeAll('dialog');
                tableIns1 = table.render({
                    elem: '#userTable2',
                    url: '/warehouse/selbaoquedrugname?drugstoreName='+data.baoqueName,
                    title: '库房药品表',
                    toolbar: '#userToolBar2',
                    cellMinWidth: 150,
                    page: 1,
                    limits: [10,20,30],
                    limit: 10,
                    cols: [[
                        {field:'rugstoreId', title:'ID',align:'center',width:"100"}
                        ,{field:'drugstoreName', title:'名称',align:'center',width:"100"}
                        ,{field:'warehouseName', title:'仓库名称',align:'center',width:"100"}
                        ,{field:'drugstorenum', title:'仓库数量',align:'center',width:"100"}
                        ,{field:'batch', title:'批号',align:'center',width:"100"}
                        ,{fixed: 'right', title:'操作', toolbar: '#userBar2',align:'center',width:'120'}
                    ]],
                });
            });
        }
    });

    var url;
    var maxIndex;
    function openUpdate(data){
        maxIndex = layer.open({
           type: 1,
           title: '药品信息',
           content: $("#saveOrUpdateDiv"),
           area: ['800px','500px']
        });
    }


    table.on('tool(userTable2)',function (obj){
        var data = obj.data;
        var layEvent = obj.event;
        if(layEvent == 'add'){
            var drugstorenum = data.drugstorenum;
            var bujinum = $("#bqnum").val();
            layer.prompt({
                title: '请输入数量',
                formType: 0,
                value: '',
                maxlength: 140
            },function (value,index,elem){
                if(value > drugstorenum){
                    layer.msg("数量不足");
                }else{
                    layer.confirm('是否补给【'+data.drugstoreName+'】药品？',function (index){
                        $.post('/warehouse/updatedrugnumber',
                            {
                                rugstoreId: data.rugstoreId,
                                warehouseId: data.warehouseId,
                                warehouse: data.warehouseId,
                                pharmacyName: data.drugstoreName,
                                unit: data.unit,
                                sellingPrice: data.sellingPrice,
                                area: data.area,
                                type: data.type,
                                drugstorenum: value,
                                drugstoreName: data.drugstoreName,
                                produceDate: data.produceDate,
                                validDate: data.validDate,
                                jiluname:data.drugstoreName,
                                jilutype:"紧急补给药房",
                                jilunumber:value
                            },
                            function (res){
                                if(res == 1){
                                   layer.msg("出库成功！");
                                   setTimeout(function (){
                                       tableIns.reload();
                                       location.reload();
                                   },500);
                                }else{
                                    layer.msg("出库失败！");
                                    setTimeout(function (){
                                        tableIns.reload();
                                        location.reload();
                                    },500);
                                }
                        });
                    });
                }
            });
        }
    });
});